package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.CourseVo;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioProperties;
import com.xuecheng.content.domain.CourseBase;
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 课程基本信息前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;
    //分页查询所有课程
    @PostMapping("/list")
    public PageResponseResult<CourseBase> findAll(@RequestBody CourseDto dto, PageRequest pageRequest) {
        return courseBaseService.findAll(dto,pageRequest);
    }

    //上传课程封面图片
    @PostMapping("/upload")
    public ResponseResult<String> upload(MultipartFile uploadFile) throws Exception {
        return courseBaseService.upload(uploadFile);
    }

    //保存课程
    @PostMapping
    public ResponseResult<CourseBaseVo> saveCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.saveCourseBase(dto);
    }

    //根据课程id删除课程
    @DeleteMapping("/{courseId}")
    public ResponseResult deleteByCourseId(@PathVariable("courseId") Long courseId) {
        return courseBaseService.deleteByCourseId(courseId);
    }

    //根据课程id查询课程
    @GetMapping("/{courseBaseId}")
    public ResponseResult<CourseVo> findById(@PathVariable("courseBaseId")Long courseBaseId) {
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CourseVo courseVo = BeanHelper.copyProperties(courseBase, CourseVo.class);
        return ResponseResult.okResult(courseVo);
    }

    //修改课程
    @PutMapping
    public ResponseResult<CourseBaseVo> updateCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.updateCourseBase(dto);
    }

    //机构提交课程
    @GetMapping("/commit/{courseBaseId}")
    public ResponseResult commit(@PathVariable("courseBaseId") Long courseBaseId) {
        return courseBaseService.commit(courseBaseId);
    }

    //管理员审核课程
    @PostMapping("/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto) {
        return courseBaseService.approve(dto);
    }

    //根据课程id下架已发布课程
    @GetMapping("/offline/{courseId}")
    public ResponseResult offline(@PathVariable("courseId") Long courseId) throws Exception{
        return courseBaseService.offline(courseId);
    }
}
